package businesslogic.coursebl;

import java.util.ArrayList;
import java.util.Iterator;

import businesslogic.courseselectionbl.Strategy.AutoSelectServer;
import businesslogic.courseselectionbl.Strategy.AutoSelectionCreater;

import dataservice.coursedataservice.CourseDataService;
import dataservice.coursedataservice.LessonDataService;
import dataservice.datafactoryservice.DataFactory;
import dataservice.datafactoryservice.DataFactoryService;

import po.*;
import vo.*;

public class Course {
	CourseDataService courseData;
	LessonDataService lessonData;

	public Course() {
		DataFactoryService dataFactory = new DataFactory();
		courseData = dataFactory.getCourseData();
		lessonData = dataFactory.getLessonData();
	}

	public Message publishCourse(CourseVO vo) {

		CoursePO po = new CoursePO(vo.getId(), vo.getName(), vo.getCredit(),
				vo.getInstitute(), vo.getGrade(), vo.getCourseClassification());
		boolean result = courseData.insert(po);
		if (result) {
			return Message.SUCCESS;
		} else {
			return Message.NOT_SUCCESS;
		}
	}

	public Message deleteCourse(String courseId) {
		boolean result = courseData.delete(courseId);
		if (result) {
			return Message.SUCCESS;
		} else {
			return Message.NOT_SUCCESS;
		}
	}

	public Message modifyCourse(CourseVO vo) {
		CoursePO po = new CoursePO(vo.getId(), vo.getName(), vo.getCredit(),
				vo.getInstitute(), vo.getGrade(), vo.getCourseClassification());
		boolean result = courseData.update(po);
		if (result) {
			return Message.SUCCESS;
		} else {
			return Message.NOT_SUCCESS;
		}
	}

	public ArrayList<CourseVO> findCourse(Institute institute) {
		ArrayList<CoursePO> poList = courseData.find(institute);
		ArrayList<CourseVO> voList = new ArrayList<CourseVO>();
		Iterator<CoursePO> iterator = poList.iterator();
		while (iterator.hasNext()) {
			CourseVO vo = new CourseVO(iterator.next());
			voList.add(vo);
		}
		return voList;
	}

	public Message publishLesson(LessonVO vo) {
		TeacherPO teacherPO = new DataFactory().getTeacherData().find(
				vo.getTeacherVO().getId());
		LessonPO po = new LessonPO(vo.getId(), vo.getName(), vo.getCredit(),
				vo.getInstitute(), vo.getGrade(), vo.getCourseClassification(),
				vo.getTerm(), teacherPO, vo.getPlace(), vo.getTime(),
				vo.getTeachingInfo(), vo.getMaxStudent(), vo.getRealStudent());
		boolean result = lessonData.insert(po);
		if (vo.getCourseClassification() != CourseClassification.GONGGONGXUANXIUKE
				&& vo.getCourseClassification() != CourseClassification.TONGSHIJIAOYUKE
				&& vo.getCourseClassification() != CourseClassification.ZHUANYEXUANXIUKE) {
			AutoSelectServer autoSelectServer = new AutoSelectionCreater();
			boolean result1=autoSelectServer.autoSelect(po);
			if (!result1) {
				return Message.ASSIGN_ERROR;
			}
		}
		if (result) {
			return Message.SUCCESS;
		} else {
			return Message.NOT_SUCCESS;
		}
	}

	public Message deleteLesson(String lessonId, String term) {
		boolean result = lessonData.delete(lessonId, term);
		if (result) {
			return Message.SUCCESS;
		} else {
			return Message.NOT_SUCCESS;
		}
	}

	public Message modifyLesson(LessonVO vo) {
		TeacherPO teacherPO = new DataFactory().getTeacherData().find(
				vo.getTeacherVO().getId());
		LessonPO po = new LessonPO(vo.getId(), vo.getName(), vo.getCredit(),
				vo.getInstitute(), vo.getGrade(), vo.getCourseClassification(),
				vo.getTerm(), teacherPO, vo.getPlace(), vo.getTime(),
				vo.getTeachingInfo(), vo.getMaxStudent(), vo.getRealStudent());
		boolean result = lessonData.update(po);
		if (result) {
			return Message.SUCCESS;
		} else {
			return Message.NOT_SUCCESS;
		}
	}

	public LessonVO findLesson(String lessonId, String term) {
		LessonPO po = lessonData.find(lessonId, term);
		LessonVO vo = new LessonVO(po);
		return vo;
	}

	public LessonPO findLessonPO(String lessonId, String term) {
		LessonPO po = lessonData.find(lessonId, term);
		return po;
	}

	public ArrayList<LessonVO> findLesson(String teacherId) {
		ArrayList<LessonPO> poList = lessonData.find(teacherId);
		ArrayList<LessonVO> voList = new ArrayList<LessonVO>();
		Iterator<LessonPO> iterator = poList.iterator();
		while (iterator.hasNext()) {
			LessonVO vo = new LessonVO(iterator.next());
			voList.add(vo);
		}
		return voList;
	}

	public ArrayList<LessonVO> findLesson(Institute institute, String term) {
		ArrayList<LessonPO> poList = lessonData.find(institute, term);
		ArrayList<LessonVO> voList = new ArrayList<LessonVO>();
		Iterator<LessonPO> iterator = poList.iterator();
		while (iterator.hasNext()) {
			LessonVO vo = new LessonVO(iterator.next());
			voList.add(vo);
		}
		return voList;
	}

	public ArrayList<LessonPO> findLesson(CourseClassification type, String term) {
		ArrayList<LessonPO> poList = lessonData.find(type, term);
		return poList;
	}

	public ArrayList<LessonPO> findLesson(CourseClassification type,
			String term, Institute institute) {
		ArrayList<LessonPO> poList = lessonData.find(type, term, institute);
		return poList;
	}

	public ArrayList<LessonVO> findLesson(CourseClassification type,
			String term, Institute institute, Grade grade) {
		ArrayList<LessonPO> poList = lessonData.find(type, term, institute,
				grade);
		ArrayList<LessonVO> voList = new ArrayList<LessonVO>();
		Iterator<LessonPO> iterator = poList.iterator();
		while (iterator.hasNext()) {
			LessonVO vo = new LessonVO(iterator.next());
			voList.add(vo);
		}
		return voList;
	}

}
